/*
 * Created on 16/Dez/2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ceirinhashls;

import java.util.*;

/**
 * @author botelhodaniel
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PublicHubList {

	/**
	 * 
	 * @uml.property name="list"
	 * @uml.associationEnd multiplicity="(0 -1)" elementType="app.DCHub"
	 */
	private TreeSet list;

    
    public PublicHubList(){
        list = new TreeSet(new DCHubComparator());
    }
    
    public boolean addDCHub(DCHub hub){
        System.out.println(list.contains(hub.getAddress()));
        if(contains(hub)){
            /*get(hub.getAddress()).update(hub);//list.remove(hub);
            return false;*/
            hub.update(get(hub.getAddress()));
            list.remove(hub);
        }
        return list.add(hub);
    }
    /*public boolean addAll(PublicHubList hublist){
    	return list.addAll(hublist.getHubList());
    }*/
    public DCHub get(String address){
        return (DCHub)list.tailSet(address).first();
    }
    /*public TreeSet getHubList(){
    	return list;
    }*/
    public void clearList(){
    	list.clear();
    }
    
    public Iterator iterator(java.util.Comparator c){
    	if(c==null)
    		return iterator();
    	ArrayList a = new ArrayList(list);
    	Collections.sort(a,c);
    	return a.iterator();
    }
    
    public Iterator iterator(){
        return list.iterator();
    }
    
    public boolean contains(DCHub hub){
        return list.contains(hub.getAddress());
    }
    
    public Object getValueAt(int index, int element){
        Object[] obj = get(index).getData();
        return obj[element];
    }
    
    private DCHub get(int index){
        return ((DCHub) new ArrayList(list).get(index));
    }
    public int getSize(){
        return list.size();
    }
}
